<!-- 开通会员 -->
<template>
    <view class="auth-img" #default>
        <img :src="state.curItem.img" alt="">
        <view class="auth-bottom" :class="props.curIndex == 1 ? 'auth-bottom-box' : ''">
            <van-button type="primary" v-for="item in state.curItem.btn" :key="item.id" @click="openMember(item)">{{
                item.name }}</van-button>
        </view>
        <!-- <view>消耗100金币解锁</view> -->
    </view>
</template>
    
<script setup>

import { reactive, ref, getCurrentInstance, onMounted, watchEffect } from "vue"
import { getAuthorization, setAuthorization } from "@/utils/auth.js"
import { showToast } from 'vant';
import { showLoadingToast, closeToast } from 'vant';
import m1 from "@/assets/image/authImg/01.png" // 开通会员 认证
import m2 from '@/assets/image/authImg/02.png' // 开通会员 
// import m22 from '@/assets/image/authImg/022.png' // 男性解锁私聊

import m3 from '@/assets/image/authImg/03.png' // 查看访客
import m33 from '@/assets/image/authImg/033.png' // 查看访客 未开通vip

import m4 from '@/assets/image/authImg/04.png' // 隐私设置
import m44 from '@/assets/image/authImg/044.png' // 隐私设置 未开通vip

import m5 from '@/assets/image/authImg/05.png' // 一键隐身
import m55 from '@/assets/image/authImg/055.png' // 一键隐身 未开通vip

import m6 from '@/assets/image/authImg/06.png' // 我的关注
import m66 from '@/assets/image/authImg/066.png' // 无限关注 未开通vip
import m111 from '@/assets/image/authImg/111.png' // 无限关注 未开通vip

import m7 from '@/assets/image/authImg/07.png' // 发布动态
import m77 from '@/assets/image/authImg/077.png' // 发布动态 未开通会员

import m8 from '@/assets/image/authImg/08.png' // 解锁微信号
import m88 from '@/assets/image/authImg/088.png' // 解锁微信号 未开通vip

import m9 from '@/assets/image/authImg/09.png' // 男性解锁私聊
import m99 from '@/assets/image/authImg/099.png' // 男性解锁私聊 未开通vip

import { onLoad } from '@dcloudio/uni-app'
import {
    UserUnlockWX, // 解锁微信号
} from "@/api/im/index.js"

import {
    TrendUnLock, // 金币解锁发布动态
    IMLockNum,// 聊天金币
    DTLockNum, // 动态解锁金币
    WXLockNum, // 微信金币
    IMLock, // 男性聊天解锁IMLock
    TQ_VIPIMNUM, // 获取会员解锁微信次数
} from '@/api/person/index.js'

import {
    IMUserInfo,
} from "@/api/im/index.js"

let { proxy } = getCurrentInstance()

let props = defineProps({
    curIndex: {
        type: String,
        default: ''
    },
    userInfo: {
        type: Object,
        default: () => { }
    },
    IsVipImCount:{
        type:Number,
        default:0
    }
})

let emit = defineEmits(['TrendUnLockSuccess'])

let curUserInfo = JSON.parse(getAuthorization('userInfo'))
const state = reactive({
    show: false,
    showCenter: true,
    curPupData: [
        // 女性认证
        {
            id: '1',
            img: m1,
            btn: [
                {
                    id: "1",
                    name: '开通会员',
                    isHide: true,
                },
                {
                    id: "2",
                    name: '去认证',
                    isHide: true,
                }
            ]
        },
        // 开通会员
        {
            id: '2',
            img: m2,
            btn: [
                {
                    id: "1",
                    name: '开通会员',
                    isHide: true,
                }
            ]
        },
        // 查看访客
        {
            id: '3',
            img: m33,
            btn: [
                {
                    id: "1",
                    name: '开通会员',
                    isHide: true,
                }
            ]
        },
        // 隐私设置
        {
            id: '4',
            img: m4,
            btn: [
                {
                    id: "1",
                    name: '开通会员',
                    isHide: true,
                }
            ]
        },
        // 一键隐身
        {
            id: '5',
            img: m55,
            btn: [
                {
                    id: "1",
                    name: '开通会员',
                    isHide: true,
                }
            ]
        },
        // 回复评论
        {
            id: '6',
            img: m111,
            btn: [
                {
                    id: "1",
                    name: '开通会员',
                    isHide: true,
                },
            ]
        },
        // 发布动态
        {
            id: '7',
            img: m7,
            btn: [
                {
                    id: "1",
                    name: '开通会员',
                    isHide: true,
                },
                {
                    id: "3",
                    name: '消耗280金币解锁',
                    isHide: true,
                }
            ]
        },
        // 解锁微信号
        {
            id: '8',
            img: m88,
            btn: [
                {
                    id: "1",
                    name: '开通会员',
                    isHide: true,
                },
                {
                    id: "3",
                    name: '消耗280金币解锁',
                    isHide: true,
                }
            ]
        },
        // 解锁私聊
        {
            id: '9',
            img: m99,
            btn: [
                {
                    id: "1",
                    name: '开通会员',
                    isHide: true,
                },
                {
                    id: "3",
                    name: '消耗280金币解锁',
                    isHide: true,
                }
            ]
        },
    ],
    curItem: {},
    curUserInfo: curUserInfo,
    vipWxNum:0,
    wxImId:""
})

import {
    ChatLockType, // 男性会员聊天解锁
} from "@/api/im/index.js"

onLoad((o) => {
})

const openMember = async (item) => {
    // return
    // 开通会员解锁微信
    if (item.id == 1 && props.curIndex == '8' && state.curUserInfo.VipState) {
        if(state.vipWxNum == 0){
            showToast('解锁次数已用完');
            return
        }else{
        let { Money, ID, WX } = props.userInfo
        let params = { "money": Money, "userID": state.curUserInfo.ID, "toUserID": ID, "wxNumber": WX, "unlockWay": 0}
        try {
            showLoadingToast({
                duration: 0,
                forbidClick: true,
                message: '解锁中',
            });
            let data = await proxy.$post(UserUnlockWX, params)
            showToast("微信解锁成功");
            closeToast();
            if (data.code == 200) {
                showToast('解锁成功');
                emit('TrendUnLockSuccess', data.data)
            }
        } catch (error) {
            closeToast();
            showToast(error.message);
        }
        return
        }
    }
    if (item.id == 1 && props.curIndex != '9') {
        uni.navigateTo({
            url: "/pages/home/person/openMemberPage/index"
        })
    }
    // 去认证
    if (item.id == 2) {
        uni.navigateTo({
            url: "/pages/home/person/authentication/index"
        })
    }
    // 消耗金币解锁
    if (item.id == 3 && props.curIndex == '3') {
        try {
            showLoadingToast({
                duration: 0,
                forbidClick: true,
                message: '解锁中',
            });
            let data = await proxy.$post(TrendUnLock, {})
            closeToast();
            if (data.code == 200) {
                emit('TrendUnLockSuccess')
            }
        } catch (error) {
            closeToast();
            showToast(error.message);
        }
    }
    // 消耗金币解锁动态
    if (item.id == 3 && props.curIndex == '7') {
        try {
            showLoadingToast({
                duration: 0,
                forbidClick: true,
                message: '解锁中',
            });
            let data = await proxy.$post(TrendUnLock, {})
            closeToast();
            if (data.code == 200) {
                emit('TrendUnLockSuccess')
                showToast('解锁成功');
            }
        } catch (error) {
            closeToast();
            showToast(error.message);
        }

    }
    // 消耗金币解锁微信号
    //     const UserUnlockWXFn = async () => {
    //     let params = {"money":280,"userID":151707,"toUserID":151734,"wxNumber":"dndnxnxnxkxj","unlockWay":1}
    //     let data = await proxy.$post(UserUnlockWX, params)
    //     if (data.code == 200) {
    //         emit('TrendUnLockSuccess')
    //     }
    // }
    // 解锁微信
    if (item.id == 3 && props.curIndex == '8') {
        let { Money, ID, WX } = props.userInfo
        let params = { "money": Money, "userID": state.curUserInfo.ID, "toUserID": ID, "wxNumber": WX, "unlockWay": 1 }
        try {
            showLoadingToast({
                duration: 0,
                forbidClick: true,
                message: '解锁中',
            });
            let data = await proxy.$post(UserUnlockWX, params)
            showToast("微信解锁成功");
            closeToast();
            if (data.code == 200) {
                showToast('解锁成功');
                emit('TrendUnLockSuccess', data.data)
            }
        } catch (error) {
            closeToast();
            showToast(error.message);
        }
    }
    // 男性用户金币解锁微信
    // if (item.id == 3 && props.curIndex == '8') {
    //     let { Money, ID, WX } = props.userInfo
    //     console.log(props.userInfo, 'userInfouserInfo222')
    //     let params = { "money": Money, "userID": state.curUserInfo.ID, "toUserID": ID, "wxNumber": WX, "unlockWay": 1 }
    //     try {
    //         showLoadingToast({
    //             duration: 0,
    //             forbidClick: true,
    //             message: '解锁中',
    //         });
    //         let data = await proxy.$post(UserUnlockWX, params)
    //         showToast("微信解锁成功");
    //         closeToast();
    //         if (data.code == 200) {
    //             showToast('解锁成功');
    //             emit('TrendUnLockSuccess', data.data)
    //         }
    //     } catch (error) {
    //         closeToast();
    //         showToast(error.message);
    //     }

    // }
    // 男性会员解锁聊天
    if (item.id == 1 && props.curIndex == '9') {
        let { ID } = props.userInfo
        if(item.name == '开通会员'){
            uni.navigateTo({
                url: "/pages/home/person/openMemberPage/index"
            })
            return
        }
        try {
            showLoadingToast({
                duration: 0,
                forbidClick: true,
                message: '解锁中',
            });
            let data = await proxy.$post(ChatLockType(ID))
            closeToast();
            if (data.code == 200) {
                showToast('解锁成功');
                emit('TrendUnLockSuccess', data.data)
            }
        } catch (error) {
            closeToast();
            showToast(error.message);
        }
    }
    if (item.id == 3 && props.curIndex == '9') {
        let { ID } = props.userInfo
        try {
            showLoadingToast({
                duration: 0,
                forbidClick: true,
                message: '解锁中',
            });
            let data = await proxy.$post(IMLock(props.userInfo.ID,'5'))
            closeToast();
            if (data.code == 200) {
                showToast('解锁成功');
                emit('TrendUnLockSuccess', data.data)
            }
        } catch (error) {
            closeToast();
            showToast(error.message);
        }
    }
}

// 获取解锁聊天金币
const IMLockNumFn = async () => {
    let data = await proxy.$get(IMLockNum)
    state.curPupData.forEach(o => {
        if (o.id == props.curIndex) {
            o.btn.forEach(oItem => {
                if (oItem.id == '3') {
                    oItem.name = "消耗" + data.data + "金币解锁"
                }
            })
        }
    })
}

const DTLockNumFn = async () => {
    let data = await proxy.$get(DTLockNum)
    state.curPupData.forEach(o => {
        if (o.id == props.curIndex) {
            o.btn.forEach(oItem => {
                if (oItem.id == '3') {
                    oItem.name = "消耗" + data.data + "金币解锁"
                }
            })
        }
    })
}

// 金币解锁查看微信号
const WXLockNumFn = async () => {
    let data = await proxy.$get(WXLockNum)
    state.curPupData.forEach(o => {
        if (o.id == props.curIndex) {
            o.btn.forEach(oItem => {
                if (oItem.id == '3') {
                    oItem.name = "消耗" + data.data + "金币解锁"
                }
            })
        }
    })
}

// 会员查看微信号次数
const TQ_VIPIMNUMFn = async () => {
    console.log(666,props.userInfo)
    let data = await proxy.$get(IMUserInfo + props.userInfo.IMId)
    // let data = await proxy.$get(TQ_VIPIMNUM)
    state.vipWxNum = data.data.IsVipImCount
}

// 获取聊天对象的IM信息
const IMUserInfoFn = async () =>{
    let data = await proxy.$get(IMUserInfo + msg.to)
    // state.toUserInfo = data.data
    // state.isAuthPup = data.data.IsImUnLock
    // state.vipWxNum = data.data.Value
}

watchEffect(() => {
    if(props.curIndex == '9'){
        if(curUserInfo.VipState){
            if(curUserInfo.Sex == 0){
                state.curPupData[8].btn[0].name = '使用会员解锁'+ props.IsVipImCount +'次'
            }else{
                state.curPupData[8].btn[0].name = '使用会员权限解锁（'+ props.IsVipImCount +'次）'
            }
            state.curPupData[8].img = m9
        }else{
            state.curPupData[8].img = m99
        }
    }
    if(props.curIndex == '8'){
        if(curUserInfo.VipState){
            state.curPupData[7].btn[0].name = '使用会员权限解锁('+ state.vipWxNum +'次)'
            state.curPupData[7].img = m8
        }else{
            // state.curPupData[7].btn =  state.curPupData[7].btn.filter( o => o.id == '1')
            state.curPupData[7].img = m88
        }
    }
    if(props.curIndex == '7'){
        if(curUserInfo.VipState){
            state.curPupData[6].btn[0].name = '使用会员解锁'+ props.IsVipImCount +'次'
            state.curPupData[6].img = m7
        }else{
            state.curPupData[6].img = m77
        }
    }
    if(curUserInfo.Sex == 0){
        if(curUserInfo.RealState){
            state.curPupData[0].btn = state.curPupData[0].btn.filter( o => o.id == '1')
        }
        if(curUserInfo.VipState){
            state.curPupData[0].btn = state.curPupData[0].btn.filter( o => o.id == '2')
        }
    }
})

onMounted(() => {
    // 获取男性解锁聊天金币数据
    if(props.curIndex == '7'){
        DTLockNumFn()
    }
    if(props.curIndex == '8'){
        WXLockNumFn()
        // 获取会员查看次数
        if(curUserInfo.VipState){
            TQ_VIPIMNUMFn()
        }
    }
    if (props.curIndex == '9') {
        IMLockNumFn()
    }    
    state.curItem = state.curPupData.find(o => o.id == props.curIndex)
})

</script>
    
<style lang="scss" scoped>
.auth-img {
    position: relative;
    background: none;
    img {
        width: 100%;
        border-radius: 16rpx;
    }
}

.auth-bottom {
    position: absolute;
    width: 100%;
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    bottom: 60rpx;

    .van-button {
        flex: none;
        width: 100%;
        font-size: 32rpx;
        height: 88rpx;
        line-height: 88rpx;
        width: calc(100% - 60rpx);
        border-radius: 80rpx;
        color: #FFD6B7;
        margin-top: 30rpx;
    }

    .van-button:nth-child(2) {
        background: none;
        color: #323233;
        border: 2rpx solid #323233;
    }
}

.auth-bottom-box {
    position: absolute;
    width: 100%;
    display: flex;
    justify-content: center;
    bottom: 60rpx;

    .van-button {
        width: 280rpx;
        font-size: 32rpx;
        line-height: 88rpx;
        height: 88rpx;
    }

    .van-button:nth-child(1) {
        margin-right: 30rpx;
    }
}
</style>
    